package tool;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

import com.base.dao.ClientBaseDao;
import com.base.dao.UserDao;
import com.base.dao._BaseDao;
import com.base.service.ClientService;


/**
 * 
 * @use java给图片添加水印文字
 * @ProjectName stuff
 * @Author <a href="mailto:mhmyqn@qq.com">mumaoqiang</a></br>
 * @Date 2012-7-19 下午10:27:54 </br>
 * @FullName com.mmq.watermark.WaterMarkUtils.java </br>
 * @JDK 1.6.0 </br>
 * @Version 1.0 </br>
 */
public class WaterMarkUtils {
	
	
	// 单例模式 start
	private volatile static WaterMarkUtils instance = null;


	private WaterMarkUtils() {

	}

	public static WaterMarkUtils getInstance() {
		if (instance == null) {
			synchronized (WaterMarkUtils.class) {// 1
				if (instance == null) {// 2
					instance = new WaterMarkUtils();// 3
				}
			}
		}
		return instance;
	}

	// 单例模式 end
	
	
	/**@deprecated
	 * 该为单例类试试
	 * 
	 * 图片添加水印
	 * @param srcImgPath 需要添加水印的图片的路径
	 * @param outImgPath 添加水印后图片输出路径
	 * @param markContentColor 水印文字的颜色
	 * @param waterMarkContent 水印的文字
	 */
	public  void mark(String srcImgPath, String outImgPath, Color markContentColor, String waterMarkContent) {
		try {
			
			// 输出文件夹不存在则创建
			File sf=new File(outImgPath);
			   if(!sf.exists() && !sf.isDirectory()){
				   sf.mkdirs();
			   }
			
            // 读取原图片信息
            File srcImgFile = new File(srcImgPath);
            Image srcImg = ImageIO.read(srcImgFile);
            int srcImgWidth = srcImg.getWidth(null);
            int srcImgHeight = srcImg.getHeight(null);
            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            //Font font = new Font("Courier New", Font.PLAIN, 12);
            Font font = new Font("宋体", Font.PLAIN, 20);  
            g.setColor(markContentColor); //根据图片的背景设置水印颜色
            
            g.setFont(font);
            int x = srcImgWidth - getWatermarkLength(waterMarkContent, g) - 30;
            int y = srcImgHeight - 30;
            //int x = (srcImgWidth - getWatermarkLength(watermarkStr, g)) / 2;
            //int y = srcImgHeight / 2;
            g.drawString(waterMarkContent, x, y);
            g.dispose();
            // 输出图片
            FileOutputStream outImgStream = new FileOutputStream(outImgPath);
            ImageIO.write(bufImg, "jpg", outImgStream);
            outImgStream.flush();
            outImgStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	
	public  void mark(String srcImgPath, String outImgPath, Color markContentColor, String waterMarkContent,String timeStr) {
		try {
			
			
			
            // 读取原图片信息
            File srcImgFile = new File(srcImgPath);
            Image srcImg = ImageIO.read(srcImgFile);
            int srcImgWidth = srcImg.getWidth(null);
            int srcImgHeight = srcImg.getHeight(null);
            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            //Font font = new Font("Courier New", Font.PLAIN, 12);
            Font font = new Font("宋体", Font.PLAIN, 30);  
            g.setColor(markContentColor); //根据图片的背景设置水印颜色
            
            g.setFont(font);
            
            int length1=g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
            int length2=g.getFontMetrics(g.getFont()).charsWidth(timeStr.toCharArray(), 0, timeStr.length());
            
            int x1 = srcImgWidth - length1 - 30;
            int x2 = srcImgWidth - length2 - 30;
            int y1 = srcImgHeight - 60;
            int y2 = srcImgHeight - 30;
            //int x = (srcImgWidth - getWatermarkLength(watermarkStr, g)) / 2;
            //int y = srcImgHeight / 2;
            g.drawString(waterMarkContent, x1, y1);
            g.drawString(timeStr, x2, y2);
            g.dispose();
            // 输出图片
            
            System.out.println(outImgPath);
            FileOutputStream outImgStream = new FileOutputStream(outImgPath);
            
            
            
            ImageIO.write(bufImg, "jpg", outImgStream);
            outImgStream.flush();
            outImgStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	
	
	
	/**
	 * 获取水印文字总长度
	 * @param waterMarkContent 水印的文字
	 * @param g
	 * @return 水印文字总长度
	 */
    public int getWatermarkLength(String waterMarkContent, Graphics2D g) {
    	System.out.println(waterMarkContent.length());
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }
    
    public static void main(String[] args) {
    	String str="四川省成都市新都区木兰镇石木路4-4号   2016-01-28 17:28";
    	
    	// 原图位置, 输出图片位置, 水印文字颜色, 水印文字
//        new WaterMarkUtils().mark("D:\\_temp\\test\\test.jpg", "D:\\_temp\\test\\test2.jpg", Color.red, "水印效果测试");
        new WaterMarkUtils().mark("D:\\_temp\\test\\test.jpg", "D:\\_temp\\test\\test2.jpg", Color.blue, "水印效果测试<br>dddddd",str);
       // String url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=H_DQfdZJP8qwjUJ_iGrapciDbrqpj1Ar9eZhkhtpO_hoFcNOR98UXvuk1jxEyKk6XpUttJUyRLg9EiGKj6SoQVK6CCkfYVI6skliynpIxWd7QIvx4YacKrhpFcpgfwJHVYVfAFANQN&media_id=TDD5K4NTMCJHmEOKtK_Aq2XkNuWziKT5deZJJ3kcF2xVWNcdGCR3QuI_MYQXJbS_";
        //new WaterMarkUtils().mark(url, "D:\\_temp\\test\\test2.jpg", Color.red, str);
        
   }
}
